
library(xtable)
library(dplyr)
library(broom)
library(skimr)

#Data loading and cleannig

#Study 1
dfe <- read.csv("surveydata_clean_study1.csv")
dfe <- subset(dfe, select = c(disc_problem.7, disc_treatment.7, immi, disc_widespr.7, nonwest, ideology))
dfe <- subset(dfe, nonwest != TRUE)
dfe <- subset(dfe, select = c(disc_problem.7, disc_treatment.7, immi, disc_widespr.7, ideology))
dfe<- dfe[complete.cases(dfe[, c("disc_problem.7", "disc_treatment.7","disc_widespr.7")]), ]

#Study 2
dfy <- read.csv("surveydata_clean_study2.csv")

#delete lines with missing wave 2
dfy <- dfy[!is.na(dfy$disc_treatment),]
dfy$donate_none[is.na(dfy$donate_none)] <- 0

#treat immi and policy as NA for people with only one response
dfy$immi <- ifelse(dfy$immi_notna > 1, dfy$immi, NA)
dfy$disc_policy <- ifelse(dfy$policy_notna > 1, dfy$disc_policy, NA)

#keeping complete cases on primary variables
dfy <- dfy[!dfy$rhetoric & !is.na(dfy$disc_treatment),]
dfy<- dfy[complete.cases(dfy[, c("disc_problem", "disc_treatment")]), ]
dfy <- subset(dfy, select = 
  c(disc_problem, disc_treatment, immi, disc_policy,donate_Mino,disc_widespr,ideology,SDO,JWB,ethnocentrism,RWA,donate_none))

#reorder treatment
dfe$disc_treatment.7 <- factor(
  dfe$disc_treatment.7,
  levels = c("control", "evidenced_stat", "evidenced_epis", "perceived_stat", "perceived_epis") )
dfy$disc_treatment <- factor(
  dfy$disc_treatment,
  levels = c("control", "thematic", "episodic") )


#Analysis

##Outcome Problem Severity
#Study 1
sums <- dfe %>%
  group_by(disc_treatment.7) %>%
  summarise(
    Mean = mean(disc_problem.7),
    SD = sd(disc_problem.7),
    n = n()
  )

sumst <- dfe %>%
  summarise(
    Mean = mean(disc_problem.7),
    SD = sd(disc_problem.7),
    n = n()
  )

sums1 <-subset(sums, select = c("Mean","SD","n"))
sums1 = (rbind(sums1, sumst))

#Study 2
sums <- dfy %>%
  group_by(disc_treatment) %>%
  summarise(
    Mean = mean(disc_problem),
    SD = sd(disc_problem),
   n = n()
)

sumst <- dfy %>%
  summarise(
    Mean = mean(disc_problem),
    SD = sd(disc_problem),
    n = n()
)

sums2 <-subset(sums, select = c("Mean","SD","n"))
sums2 [ nrow(sums2) + 1 , ] <- NA
sums2 [ nrow(sums2) + 1 , ] <- NA
sums2 = (rbind(sums2, sumst))

#merging
Treatment = c("Control", "Audit study", "Supermarket story", "Immigrant poll", "Everyday racism","Overall")
tablesum = (cbind(Treatment, sums1, sums2))

#Table
strCaption <- paste0("Perceptions of discrimination, Severity of problem")
# set up xtable output
print(xtable(tablesum, digits = c(0, 0, 2, 2, 0, 2, 2, 0), 
             align = "llcccccc",  
             caption = strCaption, label = "outcomeproblem"),
      #size = "normalsize", #Change size; useful for bigger tables "normalsize" "footnotesize"
      include.rownames = FALSE, #Don't print rownames
      include.colnames = FALSE, #We create them ourselves
      caption.placement = "top", #"top", NULL
      table.placement="H",
      type="latex",
      hline.after=NULL, 
      floating=TRUE, # whether \begin{Table} should be created (TRUE) or not (FALSE)
      sanitize.text.function = force, # Important to treat content of first column as latex function
      add.to.row = list(pos = list(-1,
                                  #5,
                                   nrow(tablesum)),
                        command = c(paste("\\toprule \n",  # NEW row
                                          " & \\multicolumn{3}{c}{\\textbf{Study 1}} & \\multicolumn{3}{c}{\\textbf{Study 2}} \\\\\n",
                                          "\\cmidrule(l){2-4} \\cmidrule(l){5-7}\n",
                                          " & Mean & Std.Dev & n & Mean & Std.Dev & n \\\\\n", # NEW row 
                                          "\\midrule \n"
                        ),
                        paste("\\bottomrule \n"  # paste is used as it is more flexible regarding adding lines
                        )
                        )
      ),
file="./tables/descriptives/outcomeproblem.tex")


##Perceptions of discrimination: Extent

#Study 1
sums <- dfe %>%
  group_by(disc_treatment.7) %>%
  summarise(
    Mean = mean(disc_widespr.7),
    SD = sd(disc_widespr.7),
    n = n()
  )

sumst <- dfe %>%
  summarise(
    Mean = mean(disc_widespr.7),
    SD = sd(disc_widespr.7),
    n = n()
)

sums1 <-subset(sums, select = c("Mean","SD","n"))
sums1 = (rbind(sums1, sumst))

#Study 2
sums <- dfy %>%
  group_by(disc_treatment) %>%
  summarise(
    Mean = mean(disc_widespr),
    SD = sd(disc_widespr),
    n = n()
  )

sumst <- dfy %>%
  summarise(
    Mean = mean(disc_widespr),
    SD = sd(disc_widespr),
    n = n()
  )

sums2 <-subset(sums, select = c("Mean","SD","n"))
sums2 [ nrow(sums2) + 1 , ] <- NA
sums2 [ nrow(sums2) + 1 , ] <- NA
sums2 = (rbind(sums2, sumst))

#merging
Treatment = c("Control", "Audit study", "Supermarket story", "Immigrant poll", "Everyday racism","Overall")
tablesum = (cbind(Treatment, sums1, sums2))

#Table
strCaption <- paste0("Perceptions of discrimination, Extent")
# set up xtable output
print(xtable(tablesum, digits = c(0, 0, 2, 2, 0, 2, 2, 0), # first zero "represents" row numbers which we skip later
             align = "llcccccc",  # align and put a vertical line (first "l" again represents column of row numbers)
             caption = strCaption, label = "outcomewidespr"),
      #size = "normalsize", #Change size; useful for bigger tables "normalsize" "footnotesize"
      include.rownames = FALSE, #Don't print rownames
      include.colnames = FALSE, #We create them ourselves
      caption.placement = "top", #"top", NULL
      table.placement="H",
      type="latex",
      hline.after=NULL, #We don't need hline; we use booktabs
      floating=TRUE, # whether \begin{Table} should be created (TRUE) or not (FALSE)
      sanitize.text.function = force, # Important to treat content of first column as latex function
      add.to.row = list(pos = list(-1,
                                   #2,
                                   nrow(tablesum)),
                        command = c(paste("\\toprule \n",  # NEW row
                                          " & \\multicolumn{3}{c}{\\textbf{Study 1}} & \\multicolumn{3}{c}{\\textbf{Study 2}} \\\\\n",
                                          "\\cmidrule(l){2-4} \\cmidrule(l){5-7}\n",
                                          " & Mean & Std.Dev & n & Mean & Std.Dev & n\\\\\n", # NEW row 
                                          "\\midrule \n"),
                        paste("\\bottomrule \n"  # paste is used as it is more flexible regarding adding lines
                        )
                        )
                        ),
file="./tables/descriptives/outcomewidespr.tex")


##Support for anti-discrimination/pro-inclusion policies

#Study 1
sums1 <- data.frame(matrix(NA,    # Create empty data frame
                          nrow = 6,
                          ncol = 3))

#Study 2
dfy2<- dfy[complete.cases(dfy[, c("disc_policy", "disc_treatment")]), ]
sums <- dfy2 %>%
  group_by(disc_treatment) %>%
  summarise(
    Mean = mean(disc_policy),
    SD = sd(disc_policy),
    n = n()
  )

sumst <- dfy2 %>%
  summarise(
    Mean = mean(disc_policy),
    SD = sd(disc_policy),
    n = n()
  )

sums2 <-subset(sums, select = c("Mean","SD","n"))
sums2 [ nrow(sums2) + 1 , ] <- NA
sums2 [ nrow(sums2) + 1 , ] <- NA
sums2 = (rbind(sums2, sumst))

#merging
Treatment = c("Control", "Audit study", "Supermarket story", "Immigrant poll", "Everyday racism","Overall")
tablesum = (cbind(Treatment, sums1, sums2))

strCaption <- paste0("Support for anti-discrimination/pro-inclusion policies")
# set up xtable output
print(xtable(tablesum, digits = c(0, 0, 2, 2, 0, 2, 2, 0), # first zero "represents" row numbers which we skip later
             align = "llcccccc",  # align and put a vertical line (first "l" again represents column of row numbers)
             caption = strCaption, label = "outcomepolicy"),
      #size = "normalsize", #Change size; useful for bigger tables "normalsize" "footnotesize"
      include.rownames = FALSE, #Don't print rownames
      include.colnames = FALSE, #We create them ourselves
      caption.placement = "top", #"top", NULL
      table.placement="H",
      type="latex",
      hline.after=NULL, #We don't need hline; we use booktabs
      floating=TRUE, # whether \begin{Table} should be created (TRUE) or not (FALSE)
      sanitize.text.function = force, # Important to treat content of first column as latex function
      add.to.row = list(pos = list(-1,
                                   #2,
                                   nrow(tablesum)),
                        command = c(paste("\\toprule \n",  # NEW row
                                          " & \\multicolumn{3}{c}{\\textbf{Study 1}} & \\multicolumn{3}{c}{\\textbf{Study 2}} \\\\\n",
                                          "\\cmidrule(l){2-4} \\cmidrule(l){5-7}\n",
                                          " & Mean & Std.Dev & n & Mean & Std.Dev & n\\\\\n", # NEW row 
                                          "\\midrule \n"),
                                    paste("\\bottomrule \n"  # paste is used as it is more flexible regarding adding lines
                                    )
                        )
      ),
      file="./tables/descriptives/outcomepolicy.tex")


##Donations

#Study 1
sums1 <- data.frame(matrix(NA,    # Create empty data frame
                           nrow = 6,
                           ncol = 3))

#Study 2 

dfy2 <- subset(dfy, donate_none!=1)
dfy2<- dfy2[complete.cases(dfy2[, c("donate_Mino", "disc_treatment")]), ]

sums <- dfy2 %>%
  group_by(disc_treatment) %>%
  summarise(
    Mean = mean(donate_Mino),
    SD = sd(donate_Mino),
    n = n()
  )

sumst <- dfy2 %>%
  summarise(
    Mean = mean(donate_Mino),
    SD = sd(donate_Mino),
    n = n()
  )

sums2 <-subset(sums, select = c("Mean","SD","n"))
sums2 [ nrow(sums2) + 1 , ] <- NA
sums2 [ nrow(sums2) + 1 , ] <- NA
sums2 = (rbind(sums2, sumst))

#merging
Treatment = c("Control", "Audit study", "Supermarket story", "Immigrant poll", "Everyday racism","Overall")
tablesum = (cbind(Treatment, sums1, sums2))

strCaption <- paste0("Donations")
# set up xtable output
print(xtable(tablesum, digits = c(0, 0, 2, 2, 0, 2, 2, 0), # first zero "represents" row numbers which we skip later
             align = "llcccccc",  # align and put a vertical line (first "l" again represents column of row numbers)
             caption = strCaption, label = "outcomedonations"),
      #size = "normalsize", #Change size; useful for bigger tables "normalsize" "footnotesize"
      include.rownames = FALSE, #Don't print rownames
      include.colnames = FALSE, #We create them ourselves
      caption.placement = "top", #"top", NULL
      table.placement="H",
      type="latex",
      hline.after=NULL, #We don't need hline; we use booktabs
      floating=TRUE, # whether \begin{Table} should be created (TRUE) or not (FALSE)
      sanitize.text.function = force, # Important to treat content of first column as latex function
      add.to.row = list(pos = list(-1,
                                   #2,
                                   nrow(tablesum)),
                        command = c(paste("\\toprule \n",  # NEW row
                                          " & \\multicolumn{3}{c}{\\textbf{Study 1}} & \\multicolumn{3}{c}{\\textbf{Study 2}} \\\\\n",
                                          "\\cmidrule(l){2-4} \\cmidrule(l){5-7}\n",
                                          " & Mean & Std.Dev & n & Mean & Std.Dev & n \\\\\n", # NEW row 
                                          "\\midrule \n"),
                                    paste("\\bottomrule \n"  # paste is used as it is more flexible regarding adding lines
                                    )
                        )
      ),
      file="./tables/descriptives/outcomedonations.tex")


##Covariates-moderators

#Study 1
#immi
dfe2<- dfe[complete.cases(dfe[, c("disc_problem.7", "disc_treatment.7", "immi","disc_widespr.7")]), ]
sumi <- dfe2 %>%
  summarise(
    Mean = mean(immi),
    SD = sd(immi),
    n = n()
  )

dfe2<- dfe[complete.cases(dfe[, c("disc_problem.7", "disc_treatment.7", "disc_widespr.7","ideology")]), ]
sumidei <- dfe2 %>%
  summarise(
    Mean = mean(ideology),
    SD = sd(ideology),
    n = n()
  )
sumcov1 = (rbind(sumi,sumidei))
sumcov1 [ nrow(sumcov1) + 1 , ] <- NA
sumcov1 [ nrow(sumcov1) + 1 , ] <- NA
sumcov1 [ nrow(sumcov1) + 1 , ] <- NA
sumcov1 [ nrow(sumcov1) + 1 , ] <- NA

#Study 2
#immi
dfy2<- dfy[complete.cases(dfy[, c("disc_problem", "disc_treatment", "immi","disc_widespr")]), ]
sumi <- dfy2 %>%
  summarise(
    Mean = mean(immi),
    SD = sd(immi),
    n = n()
  )

dfy2<- dfy[complete.cases(dfy[, c("disc_problem", "disc_treatment","disc_widespr","ideology")]), ]
sumidei <- dfy2 %>%
  summarise(
    Mean = mean(ideology),
    SD = sd(ideology),
    n = n()
  )
dfy2<- dfy[complete.cases(dfy[, c("disc_problem", "disc_treatment","disc_widespr","SDO")]), ]
sumsdo <- dfy2 %>%
  summarise(
    Mean = mean(SDO),
    SD = sd(SDO),
    n = n()
  )
dfy2<- dfy[complete.cases(dfy[, c("disc_problem", "disc_treatment","disc_widespr","JWB")]), ]
sumjwb <- dfy2 %>%
  summarise(
    Mean = mean(JWB),
    SD = sd(JWB),
    n = n()
  )
dfy2<- dfy[complete.cases(dfy[, c("disc_problem", "disc_treatment","disc_widespr","ethnocentrism")]), ]
sumethno <- dfy2 %>%
  summarise(
    Mean = mean(ethnocentrism),
    SD = sd(ethnocentrism),
    n = n()
  )
dfy2<- dfy[complete.cases(dfy[, c("disc_problem", "disc_treatment","disc_widespr","RWA")]), ]
sumrwa <- dfy2 %>%
  summarise(
    Mean = mean(RWA),
    SD = sd(RWA),
    n = n()
  )
sumcov2 = (rbind(sumi,sumidei,sumsdo,sumjwb,sumethno,sumrwa))

covnames = c("Attitudes towards non-Western immigrants", "Political ideology", "Social Dominance Orientation",
             "Just World Belief", "Ethnocentrism", "Right-wing authoritarianism")
covmod = (cbind(covnames, sumcov1, sumcov2))


strCaption <- paste0("Covariate and moderators")
# set up xtable output
print(xtable(covmod, digits = c(0, 0, 2, 2, 2, 2, 2, 2), # first zero "represents" row numbers which we skip later
             align = "llcccccc",  # align and put a vertical line (first "l" again represents column of row numbers)
             caption = strCaption, label = "outcomedonations"),
      #size = "normalsize", #Change size; useful for bigger tables "normalsize" "footnotesize"
      include.rownames = FALSE, #Don't print rownames
      include.colnames = FALSE, #We create them ourselves
      caption.placement = "top", #"top", NULL
      hline.after=NULL, #We don't need hline; we use booktabs
      floating=TRUE, # whether \begin{Table} should be created (TRUE) or not (FALSE)
      table.placement="H",
      type="latex",
      sanitize.text.function = force, # Important to treat content of first column as latex function
      add.to.row = list(pos = list(-1,
                                   #2,
                                   nrow(tablesum)),
                        command = c(paste("\\toprule \n",  # NEW row
                                          " & \\multicolumn{3}{c}{\\textbf{Study 1}} & \\multicolumn{3}{c}{\\textbf{Study 2}} \\\\\n",
                                          "\\cmidrule(l){2-4} \\cmidrule(l){5-7}\n",
                                          " & Mean & Std.Dev & n & Mean & Std.Dev & n \\\\\n", # NEW row 
                                          "\\midrule \n"),
                                    paste("\\bottomrule \n"  # paste is used as it is more flexible regarding adding lines
                                    )
                        )
      ),
      file="./tables/descriptives/covmoderators.tex")

